package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.DateUtil;
import com.qingcheng.dao.SkuMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SkuService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestClientTest  {

    @Autowired
    private  SkuMapper skuMapper;

//    @Autowired
//    private  SkuServiceImpl skuService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void add() throws IOException {
        //1.连接rest接口
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);//高级客户端对象
//2.执行添加
        IndexRequest indexRequest = new IndexRequest("sku", "doc");


        Map skuMap = new HashMap();
        skuMap.put("name", "华为p40pro  火爆上市");
        skuMap.put("brandName", "华为");
        skuMap.put("categoryName", "手机");
        skuMap.put("price", 1010221);
        skuMap.put("createTime", "2019-05-01");
        skuMap.put("saleNum", 101021);
        skuMap.put("commentNum", 10102321);
        Map spec = new HashMap();
        spec.put("网络制式", "移动4G");
        spec.put("屏幕尺寸", "5");
        skuMap.put("spec", spec);
        indexRequest.source(skuMap);

        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        int status = index.status().getStatus();
        System.out.println(status);
        //2.封装请求对象

    }

    @Test
    public void query() throws IOException {
        //1.连接rest接口
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);

        //2.查询

        SearchRequest searchRequest = new SearchRequest("sku");

        searchRequest.types("doc"); //设置查询的类型

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "手机");
        //装配条件
        searchSourceBuilder.query(matchQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits searchHits = search.getHits();
        long totalHits = searchHits.getTotalHits();
        System.out.println("记录数：" + totalHits);
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            String source = hit.getSourceAsString();
            System.out.println(source);
        }
        restHighLevelClient.close();
    }



    private static DateUtil  dateUtil;




    @Test
    public void show() throws IOException {

        //1.连接rest接口
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient = new
        RestHighLevelClient(builder);//高级客户端对象
//2.执行添加
        Map skuMap = new HashMap();
        skuMap.put("sss","1");
        List<Sku> skus = skuMapper.selectAll();
        BulkRequest bulkRequest = new BulkRequest();
         int i=0;
             for (Sku sku : skus) {
                 i++;
                 if (i<=100){
                     IndexRequest indexRequest = new IndexRequest("sku", "doc");
                     skuMap.put("name", sku.getName());
                     skuMap.put("brandName", sku.getBrandName());
                     skuMap.put("categoryName", sku.getCategoryName());
                     skuMap.put("price", sku.getPrice());
                     skuMap.put("createTime", sku.getCreateTime());
                     skuMap.put("saleNum", sku.getSaleNum());
                     skuMap.put("commentNum", sku.getCommentNum());
                     Map specMap =(Map) JSON.parseObject(sku.getSpec());
                     skuMap.put("spec",specMap);
                     skuMap.put("salaNum",sku.getSaleNum());
                     skuMap.put("commentNum",sku.getCommentNum());
                     indexRequest.source(skuMap);
                     bulkRequest.add(indexRequest);
                 }
         }

        BulkResponse index = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        int status = index.status().getStatus();
        System.out.println(status);
        //2.封装请求对象

    }


}
